package game.entities;

import game.Game;
import game.InputHandler;
import game.gfx.Colors;
import game.gfx.Font;
import game.gfx.Screen;
import game.level.Level;

public class Archer extends Player {

	private int color = Colors.get(-1, 111, 050, 543);
	private int bowColor = Colors.get(-1, 533, 444, -1);
	private boolean isHeld;
	private boolean dir;
	private long timeSinceClick = System.currentTimeMillis();
	private long timeClickNow = System.currentTimeMillis();
	private boolean startClick = true;

	public Archer(Level level, int x, int y, InputHandler input,
			String username, int fallSpdForDmg) {
		super(level, x, y, input, username, fallSpdForDmg,2);
	}

	public void tick() {
		super.tick();
	}

	public void render(Screen screen) {
		timeNow = System.currentTimeMillis();
		int xTile = 0;
		int yTile = 20;
		int walkingSpeed = 4;
		// int flipTop = (numSteps >> walkingSpeed) & 1;
		// int flipBottom = (numSteps >> walkingSpeed) & 1;
		int flipTop = 0;
		int flipBottom = 0;
		if (input.mouse.mousePosX() < 330) {
			flipTop = 1;
			flipBottom = 1;
		}

		int modifier = 8 * scale;
		int xOffset = x - modifier / 2;
		int yOffset = y - modifier / 2 - 4;
		/*
		 * if (isSwimming) { int waterColor = 0; yOffset += 4; if (tickCount %
		 * 60 < 15) { waterColor = Colors.get(-1, -1, 225, -1); } else if (15 <=
		 * tickCount % 60 && tickCount % 60 < 30) { yOffset -= 1; waterColor =
		 * Colors.get(-1, 225, 115, -1); } else if (30 <= tickCount % 60 &&
		 * tickCount % 60 < 45) { waterColor = Colors.get(-1, 115, -1, 225); }
		 * else { yOffset -= 1; waterColor = Colors.get(-1, 225, 115, -1); }
		 * screen.render(xOffset, yOffset + 3, 0 + 27 * 32, waterColor, 0x00,
		 * 1); screen.render(xOffset + 8, yOffset + 3, 0 + 27 * 32, waterColor,
		 * 0x01, 1);}
		 */
		screen.render(xOffset + modifier - (modifier * flipTop), yOffset, xTile
				+ 1 + yTile * 32, color, flipTop, scale);
		screen.render(xOffset + (modifier * flipTop), yOffset, (xTile) + yTile
				* 32, color, flipTop, scale);

		if (!isSwimming) {
			screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
					+ modifier, xTile + 1 + (yTile + 1) * 32, color,
					flipBottom, scale);
			screen.render(xOffset + (modifier * flipBottom),
					yOffset + modifier, (xTile) + (yTile + 1) * 32, color,
					flipBottom, scale);
		}

		// Heart rendering
		if (health > 0) {
			if ((double) (maxHealth / health) >= 1
					&& (double) (maxHealth / health) < 2) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 300, 500, 555), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 2
					&& (double) (maxHealth / health) < 3) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 200, 400, 444), 0x00, scale);
			} else if ((double) (maxHealth / health) >= 3
					&& (double) (maxHealth / health) < 4) {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 100, 300, 333), 0x00, scale);
			} else {
				screen.render(xOffset + 4, yOffset + 17, 31 + 29 * 32,
						Colors.get(-1, 000, 200, 222), 0x00, scale);
			}
		}

		if (username != null) {
			String usernamehp = username + " - " + health;
			Font.render(usernamehp, screen, xOffset - (usernamehp.length() - 1)
					/ 2 * 8, yOffset - 10, Colors.get(-1, -1, -1, 555), 1);
		}
		double ang = (Math.atan((240 - input.mouse.mousePosY())/(330 - input.mouse.mousePosX())))*180/Math.PI;
		if(isHeld && (timeNow - lastSwing >= 500) && !heldSwing && !input.mouse.rightIsHeld()){
			if(startClick){
				timeSinceClick = System.currentTimeMillis();
				startClick = false;
			}
			if(input.mouse.isReleased()){
				if(timeNow - timeSinceClick < 500){
					double difference = timeNow - timeSinceClick;
					if(difference < 250)
						difference = 250;
					if (input.mouse.mousePosX() < 330) {
						Arrow arrow = new Arrow(level, (int)(difference/500*20),(int)ang>0?-(180-(int)ang):180+(int)ang,x,y,input);
						level.addEntity(arrow);
					}else{
						Arrow arrow = new Arrow(level, (int)(difference/500*20),(int)ang,x,y,input);
						level.addEntity(arrow);
					}
				}else{
					if (input.mouse.mousePosX() < 330) {
						Arrow arrow = new Arrow(level, 20,(int)ang>0?-(180-(int)ang):180+(int)ang,x,y,input);
						level.addEntity(arrow);
					}else{
						Arrow arrow = new Arrow(level, 20,(int)ang,x,y,input);
						level.addEntity(arrow);
					}				
				}
				startClick = true;
				lastSwing = timeNow;
			}
		}
		if(input.mouse.isHeld()){
			isHeld = true;
		}else{
			isHeld = false;
		}
		if (input.mouse.rightIsHeld() && (timeNow - lastSwing >= 500) && !heldSwing && !input.mouse.isHeld()) {
			Hostile h = hasStruck();
			if(h != null){
				h.takeDamage(100);
				if(h.health<=0){
					level.removeEntity(h);
				}
			}
			lastSwing = timeNow;
			heldSwing = true;
			hasSwung = true;
		} else if (!input.mouse.rightIsHeld()) {
			heldSwing = false;
		}
		// System.out.println(hasSwung);
		if (hasSwung) {
			timeAftBegSwing = System.currentTimeMillis();
			if (shouldIterate) {
				begSwing = System.currentTimeMillis();
				shouldIterate = false;
			}
			if (timeAftBegSwing - begSwing <= 200) {
				if (flipTop == 1) {
					xOffset -= 8;
				} else {
					xOffset += 8;
				}
				yOffset -= 2;
				xTile += 2;
			} else {
				hasSwung = false;
				shouldIterate = true;
			}
		}
		//Bow rendering
		if (flipTop == 1) {
			if(input.mouse.isHeld()){
				if(timeNow - timeSinceClick < 100){
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, (xTile + 3) + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + yTile * 32, bowColor, flipTop, scale);
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
				}else if(timeNow - timeSinceClick < 500){
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, (xTile + 3) + 4 + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + 4 + yTile * 32, bowColor, flipTop, scale);
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + 4 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + 4 + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
				}else{
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, (xTile + 3) + 6 + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + 6 + yTile * 32, bowColor, flipTop, scale);
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + 6 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + 6 + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
				}
			}else{
				screen.render(xOffset + modifier - (modifier * flipTop),
						yOffset + 2, (xTile + 3) + yTile * 32, bowColor, flipTop,
						scale);
				screen.render(xOffset + (modifier * flipTop), yOffset + 2,
						(xTile + 2) + yTile * 32, bowColor, flipTop, scale);
				screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
						+ modifier + 2, xTile + 3 + (yTile + 1) * 32, bowColor,
						flipBottom, scale);
				screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
						+ 2, (xTile + 2) + (yTile + 1) * 32, bowColor, flipBottom,
						scale);
			}
			dir = true;
		} else {
			if(input.mouse.isHeld()){
				if(timeNow - timeSinceClick < 100){
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, xTile + 3 + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + yTile * 32, bowColor, flipTop, scale);
				}else if(timeNow - timeSinceClick < 500){
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + 4 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + 4 + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, xTile + 3 + 4 + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + 4 + yTile * 32, bowColor, flipTop, scale);
				}else{
					screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
							+ modifier + 2, xTile + 3 + 6 + (yTile + 1) * 32, bowColor,
							flipBottom, scale);
					screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
							+ 2, (xTile + 2) + 6 + (yTile + 1) * 32, bowColor, flipBottom,
							scale);
					screen.render(xOffset + modifier - (modifier * flipTop),
							yOffset + 2, xTile + 3 + 6 + yTile * 32, bowColor, flipTop,
							scale);
					screen.render(xOffset + (modifier * flipTop), yOffset + 2,
							(xTile + 2) + 6 + yTile * 32, bowColor, flipTop, scale);
				}
			}else{
				screen.render(xOffset + modifier - (modifier * flipBottom), yOffset
						+ modifier + 2, xTile + 3 + (yTile + 1) * 32, bowColor,
						flipBottom, scale);
				screen.render(xOffset + (modifier * flipBottom), yOffset + modifier
						+ 2, (xTile + 2) + (yTile + 1) * 32, bowColor, flipBottom,
						scale);
				screen.render(xOffset + modifier - (modifier * flipTop),
						yOffset + 2, xTile + 3 + yTile * 32, bowColor, flipTop,
						scale);
				screen.render(xOffset + (modifier * flipTop), yOffset + 2,
						(xTile + 2) + yTile * 32, bowColor, flipTop, scale);
			}
			dir = false;
		}
		xTile -= 2;
	}

	public Hostile hasStruck() {
		//System.out.println("SWING");
		for (Entity e : level.getEntities()) {
			if (e.hasHitbox && e.isEnemy) {
				if (!dir) { //Right
					for (int bowX = this.x + 15; bowX <= this.x + 24; bowX++) {
						int bowY = this.y + 9;
						if (bowX <= e.x + 5 * Game.SCALE
								&& bowX >= e.x
								&& bowY >= e.y
								&& bowY <= e.y + 15 * Game.SCALE) {
							return (Hostile)e;
						}
						bowY = this.y + 12;
						if (bowX <= e.x + 5 * Game.SCALE
								&& bowX >= e.x
								&& bowY >= e.y
								&& bowY <= e.y + 15 * Game.SCALE) {
							return (Hostile)e;
						}
					}
					// UL corner of bow = this.x + 8, this.y + 9
					// BR corner of bow = this.x + 21, this.y + 12
					// x + 5, x + 12
					// 0, 16
					// ^ Rogue hitbox
				} else { //THIS SIDE DOES NOT WORK
					for (int bowX = this.x+1; bowX >= this.x-10; bowX--) {
						int bowY = this.y + 9;

						if (bowX <= e.x + 5 * Game.SCALE
								&& bowX >= e.x
								&& bowY >= e.y
								&& bowY <= e.y + 15 * Game.SCALE) {
							return (Hostile)e;
						}
						bowY = this.y + 12;
						if (bowX <= e.x + 5 * Game.SCALE
								&& bowX >= e.x
								&& bowY >= e.y
								&& bowY <= e.y + 15 * Game.SCALE) {
							return (Hostile)e;
						}
					}
				}
			}
		}
		return null;
	}

}
